package com.test1;


import ch.qos.logback.core.db.DBHelper;
import com.test1.bean.Person;
import com.test1.bean.User;
import com.test1.config.MyConfig;
import com.test1.controller.TestController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 主程序类
 *  @SpringBootApplication:     @SpringBootConfiguration / @EnableAutoConfiguration / @ComponentScan
 */
@Slf4j
@SpringBootApplication
public class LyApp {

    public static void main(String[] args) {
        //1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(LyApp.class, args);

        //2、查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            // System.out.println(name);
        }

        log.info("Bean 总数为：{}", run.getBeanDefinitionCount());
        log.info("Bean 总数(type)为：{}", run.getBeanNamesForType(CacheAspectSupport.class).length);
        log.info("Bean 总数(name)为：{}", run.getBeanNamesForType(WebMvcProperties.class).length);
        log.info("Bean 获取指定类型的组件：{}", run.getBean(MyConfig.class));
        log.info("Bean 的单例验证：{}", run.getBean("testController", TestController.class) == run.getBean("testController", TestController.class));

        // //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
        // System.out.println(bean.user01() == bean.user01());
        //
        // User user01 = run.getBean("user01", User.class);
        // Pet tom = run.getBean("tom22", Pet.class);
        // System.out.println("用户的宠物：" + (user01.getPet() == tom));

        //5、获取组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        System.out.println("======");
        for (String s : beanNamesForType) {
            System.out.println(s);
        }

        log.info("容器 @Import形式 DBHelper：{}", run.getBean(DBHelper.class));
        log.info("容器 @Bean形式 User：{}", run.containsBean("tom"));
        log.info("容器 @Bean形式 user01：{}", run.containsBean("user01"));
        log.info("容器 配置文件形式：{}", run.containsBean("haha"));
        log.info("容器 属性配置文件导入：{}", run.getBean(Person.class));

    }
}

